<template>
  <transition name="bottom">
    <div v-show="show">
      <MaskLayer class="mask-layer" :show="true"></MaskLayer>
      <div class="container">
        <div class="drop-header">
          <div @click="$emit('close')">取消</div>
          <div>{{title}}</div>
          <div class="drop-sure" @click="$emit('confirm', selectedIndex)">确定</div>
        </div>
        <div class="drop-content">
          <div v-for="(option, index) in options"
               :key="index"
               :class="selectedIndex === index ? 'drop-selected' : ''"
               @click="selectedIndex = index">{{option}}</div>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
import MaskLayer from '../MaskLayer'

export default {
  props: {
    show: Boolean,
    title: String,
    defaultIndex: Number,
    options: {
      type: Array,
      validator (value) {
        return value.length > 0
      }
    }
  },
  data () {
    return {
      selectedIndex: this.defaultIndex || 0
    }
  },
  components: {
    MaskLayer
  }
}
</script>

<style rel="stylesheet/less" lang="less" scoped>
  .container {
    position: fixed;
    bottom: 0;
    right: 0;
    left: 0;
    z-index: 100;
    background-color: #fff;
  }

  .bottom-enter-active, .bottom-leave-active {
    transition: all .3s ease;

    .mask-layer {
      transition: all .3s ease;
      opacity: 1;
    }

    .container {
      transition: all .3s ease;
      transform: translate3d(0, 0, 0);
    }

  }

  .bottom-enter, .bottom-leave-to {
    transition: all .3s ease-out;

    .mask-layer {
      transition: all .3s ease;
      opacity: 0!important;
    }

    .container {
      transition: all .3s ease;
      transform: translate3d(0, 100%, 0)!important;
    }
  }

  .drop-header {
    display: flex;
    padding: 9px 0;
    border-bottom: 1px solid #d7d7d7;

    > div {
      padding: 0 10px;
      font-size: 14px;
    }

    > div:nth-child(2) {
      flex: 1;
      font-size: 16px;
      text-align: center;
    }
  }

  .drop-content {
    min-height: 80px;
    max-height: 200px;
    overflow-y: scroll;
    background-color: #fafafa;

    > div {
      line-height: 40px;
      text-align: center;
    }
  }

  @theme-color: #7fbe87;

  .drop-sure {
    color: @theme-color;
  }

  .drop-selected {
    background-color: #fff;
    color: @theme-color;
  }

</style>
